package com.eazy.lkv2.web.controller.system;


import java.io.IOException;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.eazy.lkv2.web.controller.BaseController;
import com.eazy.lkv2.web.entity.Permission;
import com.eazy.lkv2.web.entity.RolePermission;
import com.eazy.lkv2.web.service.system.SystemMenuService;
import com.eazy.lkv2.web.service.system.SystemPermissionService;
import com.eazy.lkv2.web.service.system.SystemRolePermissionService;

/**
 * 菜单管理
 * @author apple
 * @date 2016.6.18
 */
@Controller
@RequestMapping("/permission")
public class SystemMenuController extends BaseController<Object,Integer> {

	@Autowired
	private SystemPermissionService systemPermissionService;
	@Autowired
	private SystemRolePermissionService rolePermissionService;
	@Autowired
	private SystemMenuService systemMenuService;
	
	/**
	 * 获取权限列表
	 */
	@RequestMapping(value = "list" , method = { RequestMethod.POST, RequestMethod.GET })
	public ModelAndView list() {
		return getSuccess("admin/menu_list", "dataMenu", systemMenuService.select());
	}
	
	
	/**
	 * tree 菜单
	 */
	@RequestMapping(value = "tree" , method = { RequestMethod.POST, RequestMethod.GET })
	public void tree() throws IOException {
		List<Map<String,Object>> menu =  systemMenuService.select();
		renderJson(systemMenuService.treeBiz(menu));
	}
	
	/**
	 * 第二层tree
	 */
	@RequestMapping(value = "tree/{pid}" , method = { RequestMethod.POST, RequestMethod.GET })
	public void tree2(@PathVariable String pid) throws IOException {
		List<Map<String, Object>> permission =  systemMenuService.selectSubMenu(pid);
		renderJson(systemMenuService.tree2Biz(permission));
	}
	
	/**
	 * 添加子菜单
	 */
	@RequiresPermissions("sys:subperm:add")
	@RequestMapping(value = "/addSubMenu",  method = RequestMethod.POST)
	public String addSubMenu(Permission permission) {
		systemMenuService.create(permission);
		return "redirect:/permission/list";
	}
	
	/**
	 * 获取权限列表子菜单
	 */
	@RequestMapping(value = "submenu/{pid}" , method = { RequestMethod.POST, RequestMethod.GET })
	public ModelAndView submenu(@PathVariable String pid) {
		addObject("pid", pid);
		return getSuccess("admin/submenu_list","dataMenu",systemMenuService.selectSubMenu(pid));
	}
	
	/**
	 * 修改子菜单
	 */
	@RequiresPermissions("sys:subperm:update")
	@RequestMapping(value = "/updateSubMenu",  method = RequestMethod.POST)
	public String updateSubMenu(Permission permission) {
		systemMenuService.update(permission);
		return "redirect:/permission/list";
	}
	
	/**
	 * 删除子菜单
	 */
	@RequiresPermissions("sys:subperm:delete")
	@RequestMapping(value = "delsubmenu/{sid}/{pid}" , method = { RequestMethod.POST, RequestMethod.GET })
	public String delsubmenu(@PathVariable String sid,@PathVariable String pid) throws IOException {
		// 先删除关联表
		RolePermission rolePermission = new RolePermission();
		rolePermission.setPermissionId(Integer.valueOf(sid));
		rolePermissionService.remove(rolePermission);
		
		// 在删除主表
		systemMenuService.removeById(sid);
		
		return "redirect:/permission/submenu/" + pid;
	}
	
	/**
	 * 添加主菜单
	 */
	@RequiresPermissions("sys:perm:add")
	@RequestMapping(value = "/addMenu",  method = RequestMethod.POST)
	public String addMenu(Permission permission) {
		systemMenuService.create(permission);
		return "redirect:/permission/list";
	}
	
	/**
	 * 跳转到添加菜单页面
	 */
	@RequestMapping(value = "/toAddMenu", method = { RequestMethod.POST, RequestMethod.GET })
	public ModelAndView toAddMenu(HttpServletRequest request) {
		return new ModelAndView("admin/menu_add");
	}
	
	/**
	 * 跳转到添加子菜单页面
	 */
	@RequestMapping(value = "/toAddSubMenu/{pid}", method = { RequestMethod.POST, RequestMethod.GET })
	public ModelAndView toAddSubMenu(@PathVariable String pid) {
		return new ModelAndView("admin/submenu_add","pid",pid);
	}
	
	/**
	 * 跳转到编辑子菜单页面
	 */
	@RequestMapping(value = "/toEditSubMenu/{id}/{pid}", method = { RequestMethod.POST, RequestMethod.GET })
	public ModelAndView toEditSubMenu(@PathVariable String id,@PathVariable String pid) {
		Permission permission = systemMenuService.findById(id);
		addObject("id", id);
		addObject("pid", pid);
		return getSuccess("admin/submenu_update",permission);
	}
}
